package com.example.demo.controller;

import com.example.demo.commom.AjaxResult;
import com.example.demo.commom.MergeCode;
import com.example.demo.commom.ProcessString;
import com.example.demo.compile.Answer;
import com.example.demo.compile.Question;
import com.example.demo.compile.Task;
import com.example.demo.entity.Topic;
import com.example.demo.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:龙宝
 * Date:2023-10-09
 * Time:10:01
 */
@RestController
@RequestMapping("/topic")
public class TopicController {
    @Autowired
    private TopicService topicService;
    @Autowired
    private Question question;
    /**
     *
     * @param id 题目id
     * @param code 用户提交的代码
     * @return
     */
    @RequestMapping("/saveSubmit")
    public AjaxResult topicRun(Integer id,String code) {
        //参数校验
        if(id == null || id <= 0) {
            return AjaxResult.fail("题目参数有误，搜索不到！");
        }
        if(!StringUtils.hasLength(code)) {
            return AjaxResult.fail("提交代码参数有误！");
        }
        //1、TODO 识别是那个用户提交的代码暂时未做


        //2、代码拼接
        //2.1、查询出这个题目对应的标准代码答案 和测试用例集合
        String positiveSolution = topicService.getPositive(id);
        if(!StringUtils.hasLength(positiveSolution)) {
            return AjaxResult.fail("参数有误，搜索不到该题目对应的标准代码");
        }
        //2.2拼接代码
        String finCode = MergeCode.mergeCode(code,positiveSolution);

        Task task = new Task();
        question.setCode(finCode);
        //3、运行提交的代码
        Answer answer = task.CompileAndRun(question);
        if(answer.getCode() == 0) {
            return AjaxResult.success("运行成功！");
        } else {
            return AjaxResult.fail(answer.getMsgReason(), answer.getCode());//0-》运行编译都ok，1表示编译出错，2表示运行出错（会抛异常滴,3表示代码中有违规代码,4表示可以运行，但是用例没有完全通过~
        }
    }

    /**
     * 自动填充代码模版 和题目描述 和题目的难度等级 和通过率
     * @param id 题目名id
     * @return
     */
    @RequestMapping("/topicInfo")
    public AjaxResult codeTemplate(Integer id) {
        //1、参数校验
        if(id == null || id <= 0) {
            return AjaxResult.fail("参数有误");
        }
        //2、查询题目对应的信息
        Topic topic = topicService.getTopicInfo(id);
        if(topic == null) {
            return AjaxResult.fail("参数有误");
        }
        //3、对Topic进行处理
        HashMap<String,Object> map = ProcessString.processString(topic);
        return AjaxResult.success(map);
    }


    /**
     *
     * @param index 当前页码数
     * @param size 当前页面可显示的最大条数
     * @return
     */
    @RequestMapping("/topicList")
    public AjaxResult topicList(Integer index,Integer size) {
        //参数矫正
        if(index == null || index < 1) {
            index = 1;
        }
        if(size == null || size < 5 ) {
            size = 5;
        }
        if(size > 100) {
            size = 100;
        }
        //计算偏移量
        int offset = (index - 1) * size;
        //查询
        List<Topic> list = topicService.getTopicList(size,offset);

        //查询计算最大的页码数
        int count = topicService.getTopicCount();
        int pcount = 1;
        if(count % size == 0) {
            pcount = count / size;
        } else {
            pcount = count / size;
            pcount += 1;
        }

        HashMap<String,Object> map = new HashMap<>();
        map.put("list",list);
        map.put("pcount",pcount);
        return AjaxResult.success(map);
    }

}
